package xyz.iguilty.documents.example.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import xyz.iguilty.documents.common.constant.DocumentConstant;
import xyz.iguilty.documents.common.enums.PicturePosition;
import xyz.iguilty.documents.doc.model.WordModel;
import xyz.iguilty.documents.example.entity.Demo;
import java.util.*;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/excel")
    public String export(Model model){
        List<Demo> demos = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Demo demo = new Demo();
            demo.setId(i);
            demo.setName(UUID.randomUUID().toString());
            demo.setCreateTime(new Date());
            demo.setStatus(i%2==0?1:0);
            try {
                demo.setAttachment("static/icon.png");
            } catch (Exception e) {
                e.printStackTrace();
            }
            demos.add(demo);
        }
        model.addAttribute(DocumentConstant.FILENAME,"示范excel报表");
        model.addAttribute("title","示范列表");
        model.addAttribute("demos",demos);
        return "demo";
    }

    @RequestMapping("/doc")
    public ModelAndView doc(){
        WordModel model = new WordModel();
        List<Demo> demos = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Demo demo = new Demo();
            demo.setId(i);
            demo.setName(UUID.randomUUID().toString());
            demo.setCreateTime(new Date());
            demo.setStatus(i%2==0?1:0);
            try {
//                demo.setAttachment("http://static.iguilty.xyz/icon.png");
//                demo.setAttachment(new ClassPathResource("static/icon.png").getURL().getPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
            demos.add(demo);
        }
        model.addHyperlinkText("icon","头像","http://static.iguilty.xyz/icon.png");
        model.addColorfulText("title","测试word视图","ee0088");
        model.addAttribute("createTime",new Date());
        model.setExportFileName("测试word视图");
        model.addTable("demos",demos);
        model.addPicture("avatar","static/icon.png", PicturePosition.CLASSPATH,100,100);
        return new ModelAndView("example",model);
    }
}
